/**
 * @description API数据源 选择器；用于在页面设计时，绑定控件数据源
 */
 Rsd.define('Rsd.tool.DataSourceSelector', {
    extend: 'Rsd.form.Text',
    xtype: 'ds-selector',
    requires: [],
    selector:{
        viewerType:'Rsd.zen.dialog.BizListDialog',
        modelType: 'Rsd.UnionApp.DataSource.Model.AppApiData',
        titleField: 'Name',
        where:[
            {
                name:'SiteId',
                op:'equals',
                value:Rsd.app.site.Id
            }
        ],
        searchColumns: [
            {text: '名 称', name: 'Name',width:350}, 
        ], 
        newButtons:[{text: '刷新数据源', handler:'btn_refesh_ds'}],
        gridColumns: [
            {
                "text": "序号",
                "xtype": "index",
                "width": 40,
                "index": 0
            },
            {
                "name": "Name",
                "dataIndex": "Name",
                "sortable": false,
                "text": "名称",
                "index": 22,
                "exist": true,
                "xtype": "string",
                "format":"formatLabels",
                "width": 100,
                "nobr": true
            },
            {
                "name": "ObjectType",
                "dataIndex": "ObjectType",
                "sortable": false,
                "text": "API数据源类型",
                "index": 26,
                "exist": true,
                "xtype": "enum",
                "enum": "Rsd.UnionApp.DataSource.Core.DataSourceType",
                "valueMember": "code",
                "width": 50,
                "nobr": true
            }, 
            {
                "name": "Api",
                "dataIndex": "Api",
                "sortable": false,
                "text": "API模板",
                "index": 28,
                "exist": true,
                "xtype": "string",
                "format":"formatApi",
                "width": 150,
                "nobr": true
            },
            {
                "name": "Description",
                "dataIndex": "Description",
                "sortable": false,
                "text": "说明",
                "index": 30,
                "exist": true,
                "xtype": "string",
                "width": 150,
                "nobr": true
            } 
        ],
        opColumns: [
            {
                name: 'space',
                text: '',
                xtype: 'template',
                width: 5,
                fixed:true,
                hideable: false,
                sortable: false
            }
        ],
    },
    
    /*
       *
       * */
    constructor: function constructor(config) {

        config = config || {};
        this.apply(config);
        console.log('document.getElementById(ds_selector_service)');
    },
    /**
     * 
     */
    btn_refesh_ds:function btn_refesh_ds() {
       Rsd.popup('btn_refesh_ds');
    },
    /**
     * 
     * @param {*} row 
     * @returns 
     */
     formatLabels:function formatLabels(row)
     {
         var lables = [Rsd.newLine()];
         var arr = (row["Labels"]||'').split(',');
         for(var i in arr)
         {
             if(Rsd.isEmpty(arr[i]))
             {
                 continue;
             }
             lables.push(Rsd.label(arr[i])); 
         }
         
         return lables;
     },
    /**
     * 
     */
    formatApi:function formatApi(row)
    {
        return [Rsd.button('查看数据',function(){

            Rsd.request({url:row["Api"],method:"GET"},{error:function(err){alert(err);}},function(resp){

                if(resp.success)
                {
                    var _data = Rsd.unescape(resp.data);
                    Rsd.create('Rsd.zen.dialog.JsonFormDialog',{title:'查看数据['+row['Name']+']'}).showDialog().load(_data);
                }
                else
                {
                    Rsd.alert(resp.msg);
                }
                
            });
           
        })];
            
    }
 
});
